<script setup>
import {defineProps, ref,defineEmits} from "vue";
import {
  insertAssignment,
  insertAssignmentQuestionOptions,
  insertAssignmentQuestions
} from "../../../api/course/course.js";
import {ElMessage} from "element-plus";
import router from "../../../router/index.js";


// 获取关闭弹窗
const isInsertAssignment = ref(false);
const props = defineProps(['isInsertAssignment','practices']);
isInsertAssignment.value=props.isInsertAssignment;
// 触发父类方法
console.log(props.practices);
const  emit=defineEmits(['close']);

// 上传作业信息
const handleInertAssignment = () => {
  console.log(props.practices);
  assignment.value.courseId=localStorage.getItem('courseId');
  console.log(assignment.value);
    // 判断作业题目和答案是否为空
    for(let i=0;i<props.practices.length;i++){
      for(let j=0;j<props.practices[i].content.length;j++){
        if(props.practices[i].content[j].content==''){
          ElMessage.error('有作业题目为空,无法上传');
          return;
        }
        if(props.practices[i].content[j].answer==''){
          ElMessage.error('有作业答案为空,无法上传');
          return;
        }
      }
    }
  //   上传作业信息
  const newContentArray = props.practices.flatMap(item =>
      item.content.map(({ answer, content, id, type }) => ({ answer, content, id, type }))
  );
  // 作业选项信息解析
  const optionsArray = props.practices.flatMap(item =>
      item.content
          .filter(obj => obj.option)  // 过滤掉没有 option 字段的对象
          .map(({ option }) => option)  // 提取 option 字段
  );
  console.log(optionsArray);
  console.log(newContentArray);
  // 上传作业题目
  console.log(assignment.value);
  insertAssignment(assignment.value).then(res=>{
    assignment.value.id=res.data;
  //   上传作业题目
    insertAssignmentQuestions(assignment.value.id,newContentArray).then(res=>{
      const ids=res.data;
      console.log(ids);
      for(let i=0;i<ids.length;i++){
        optionsArray[i].assignmentQuestionId=ids[i];
      }
      console.log(optionsArray);
      insertAssignmentQuestionOptions(optionsArray).then(res=>{
        emit('close');
        ElMessage.success('作业上传成功');
        router.push("/course/classmanage/exams");
      });
    })
  })
}

// assignment对象
const assignment = ref({
  id:'',
  courseId:'',
  name: '',
  startTime:'',
  endTime:'',
})


</script>

<template>
  <el-dialog
      v-model="isInsertAssignment"
      title="填写作业信息"
      width="500"
      align-center
  >
    <el-form
        :label-position="'left'"
        label-width="auto"
        :model="assignment"
        style="max-width: 600px"
    >
      <el-form-item label="作业名称">
        <el-input v-model="assignment.name"  placeholder="请输入作业名称"/>
      </el-form-item>

      <div class="demo-time-range">
        <el-form-item label="作业开始时间">
          <el-date-picker
              v-model="assignment.startTime"
              type="datetime"
              placeholder="开始时间"
          />
        </el-form-item>
        <el-form-item label="作业结束时间">
          <el-date-picker
              v-model="assignment.endTime"
              type="datetime"
              placeholder="结束时间"
          />
        </el-form-item>
      </div>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="emit('close')">取消</el-button>
        <el-button type="primary" @click="handleInertAssignment">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped lang="less">




</style>